package com.dinner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.DiCar;
import com.dinner.entity.DiMenu;
import com.dinner.entity.DiSeat;
import com.dinner.mapper.DiCarMapper;
import com.dinner.response.Result;
import com.dinner.service.DiCarService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/di-car")
public class DiCarController extends JSONAuthentication {
    @Autowired
    DiCarMapper diCarMapper;
    @Autowired
    DiCarService diCarService;
    @ApiOperation("向购物车添加菜品")
    @PostMapping("/add")
    public void addSeat(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId,@RequestParam("menuId") int menuId) throws Exception{
       QueryWrapper wrapper=new QueryWrapper();
       wrapper.eq("user_id",userId);
       wrapper.eq("menu_id",menuId);
       List<DiCar> list=diCarMapper.selectList(wrapper);
      if(list.size()>0){
          Result error = Result.error().message("已添加购物车,请勿重复添加");
          this.WriteJSON(request, response, error);
      }else{
          DiCar diCar=new DiCar();
          diCar.setUserId(userId);
          diCar.setMenuId(menuId);
          if(diCarMapper.insert(diCar)>0){
              Result ok = Result.ok();
              this.WriteJSON(request, response, ok);
          }
          Result error = Result.error().message("添加失败");
          this.WriteJSON(request, response, error);
      }
    }
    @ApiOperation("根据购物车id修改菜品数量")
    @PostMapping("/updNum")
    public void updCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("menuNum") int menuNum) throws Exception{
       DiCar diCar=diCarMapper.selectById(id);
        diCar.setMenuNum(menuNum);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        diCar.setUpdTime(timestamp);
        if(diCarMapper.updateById(diCar)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除购物车菜品")
    @PostMapping("/del")
    public void delCarMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=diCarMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看购物车")
    @PostMapping("/lookMenu")
    public void lookMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size ,@RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        int total=diCarMapper.selectCount(wrapper);
        List<DiCar> list=diCarService.findCar(current,size,userId);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无菜品，去叫商家添加一个吧");
            this.WriteJSON(request, response, error);
        }
    }

}

